package cn.com.surker.lab.util;

import cn.com.surker.util.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.*;

import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;

/**
 * @Author lucky
 * @create 2025年03月13 17:32
 * @desc
 */
public class LevelExcelUtil {

    /**
     * 解下标准配合比数据
     * @param sheet
     * @param keyList 列值
     * @return
     */
    public static List<Map<String, Object>> analysisLevelSheet(Sheet sheet, List<String> keyList) {
        if (sheet == null || keyList == null || keyList.size() == 0)
            return null;

        Map<Integer, String> columnMap = new LinkedHashMap<>();
        Integer lastRowNum = sheet.getLastRowNum();//获取最后一行
        Row firstRow = sheet.getRow(1);//获取标题行
        Integer lastFirstCellNum = StringUtils.convertInteger(firstRow.getLastCellNum());
        for (int i = 0; i < lastFirstCellNum; i++) {
            String cellVal = firstRow.getCell(i).getStringCellValue();
            if (!StringUtils.isNull(cellVal)) {
                cellVal = cellVal.replaceAll(" ", "").replaceAll("\n", "")
                        .replaceAll("（", "(").replaceAll("）", ")");
                if (keyList.contains(cellVal))
                    columnMap.put(i, cellVal);
            }

        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (int i = 2; i < lastRowNum; i++) {
            Row row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            Map<String, Object> tempMap = new HashMap<>();
            for (Integer cellKey : columnMap.keySet()) {
                Cell cell = row.getCell(cellKey);
                Object cellVal = null;
                if (cell != null) {
                    if (cell.getCellType() == STRING)
                        cellVal = cell.getStringCellValue();
                    else if (cell.getCellType() == NUMERIC)
                        cellVal = cell.getNumericCellValue();
                }

                tempMap.put(columnMap.get(cellKey), cellVal);
            }
            resultList.add(tempMap);
        }
        return resultList;
    }

//    private Object getCellVal(Cell cell) {
//        switch (cell.getCellType()) {
//            case NUMERIC:
//                double numericValue = cell.getNumericCellValue();
//                return numericValue;
//                break;
//            case STRING:
//                String stringValue = cell.getStringCellValue();
//                break;
//            case FORMULA:
//                double formulaResult = cell.getNumericCellValue(); // 对于公式，通常以数字形式存储结果
//                break;
//            case BOOLEAN:
//                boolean boolValue = cell.getBooleanCellValue();
//                return boolValue;
//            case ERROR:
//                byte errorValue = cell.getErrorCellValue(); // 获取错误代码，例如 #VALUE! 为 0x0024
//                return errorValue;
//            case BLANK: // 处理空白类型（空单元格）
//                return "";
//            default: // 处理未知类型（理论上不太可能发生，除非POI库更新导致新的类型被引入）
//                return "";
//        }
//    }

}
